/*
 * Copyright (C) 2012 Le Tuan Anh <anh.takaji@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package action;

import com.opensymphony.xwork2.ActionSupport;
import ezcms.exceptions.PresentationException;
import java.io.File;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;
import util.UploadUtil;

/**
 *
 * @author Le Tuan Anh <anh.takaji@gmail.com>
 */
public class Test extends ActionSupport implements ServletRequestAware {

    private HttpServletRequest hsr;
    private File upload;
    private String uploadFileName;
    private String resourceLink;

    @Override
    public String execute() throws PresentationException {
        if (upload == null) {
            return ERROR;
        } else {
            System.out.println("Upload Filename: " + getUploadFileName());
            String savedFile = UploadUtil.saveFile(hsr.getServletContext(), upload, getUploadFileName());
            setResourceLink(savedFile);
            return SUCCESS;
        }
    }

    /**
     * @return the upload
     */
    public File getUpload() {
        return upload;
    }

    /**
     * @param upload the upload to set
     */
    public void setUpload(File upload) {
        this.upload = upload;
    }

    @Override
    public void setServletRequest(HttpServletRequest hsr) {
        this.hsr = hsr;
    }

    /**
     * @return the resourceLink
     */
    public String getResourceLink() {
        return resourceLink;
    }

    /**
     * @param resourceLink the resourceLink to set
     */
    public void setResourceLink(String resourceLink) {
        this.resourceLink = resourceLink;
    }

    public String getUploadFileName() {
        return uploadFileName;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }
}
